package cn.dubito.miaosha.service;

import cn.dubito.miaosha.controller.vo.CodeMsg;
import cn.dubito.miaosha.dao.SeckillUserDao;
import cn.dubito.miaosha.domain.SeckillUser;
import cn.dubito.miaosha.exception.SeckillException;
import cn.dubito.miaosha.param.LoginParam;
import cn.dubito.miaosha.redis.RedisService;
import cn.dubito.miaosha.redis.SeckillUserKey;
import cn.dubito.miaosha.utils.MD5Util;
import cn.dubito.miaosha.utils.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liliang
 * @description:
 * @date 2018/3/31.
 */
@Service
public class SeckillUserService {

    public static final String COOKIE_NAME_TOKEN = "token";

    @Autowired
    private SeckillUserDao seckillUserDao;

    @Autowired
    private RedisService redisService;

    public SeckillUser getById(long id) {
        SeckillUser user = redisService.get(SeckillUserKey.getById, String.valueOf(id), SeckillUser.class);
        if (user != null) {
            return user;
        }
        user = seckillUserDao.getById(id);
        if (user != null) {
            redisService.set(SeckillUserKey.getById, String.valueOf(id), user);
        }
        return user;
    }

    public boolean updatePassword(String token, long id, String passwordNew) {
        SeckillUser user = getById(id);
        if (user == null) {
            throw new SeckillException(CodeMsg.MOBILE_NOT_EXIST);
        }
        SeckillUser seckillUser = new SeckillUser();
        seckillUser.setId(id);
        seckillUser.setPassword(MD5Util.passToDb(passwordNew, user.getSalt()));
        seckillUserDao.update(seckillUser);
        // 删除缓存
        redisService.delete(SeckillUserKey.getById, String.valueOf(id));
        // 更新 token 缓存
        user.setPassword(seckillUser.getPassword());
        return redisService.set(SeckillUserKey.token, token, user);
    }

    public boolean login(HttpServletResponse response, LoginParam param) {
        if (param == null) {
            throw new SeckillException(CodeMsg.SERVER_ERROR);
        }
        String mobile = param.getMobile();
        String password = param.getPassword();
        SeckillUser user = getById(Long.parseLong(mobile));
        if (user == null) {
            throw new SeckillException(CodeMsg.MOBILE_NOT_EXIST);
        }
        String salt = user.getSalt();
        String pass = MD5Util.passToDb(password, salt);
        String passwordDB = user.getPassword();
        if (!pass.equals(passwordDB)) {
            throw new SeckillException(CodeMsg.PASSWORD_ERROR);
        }

        // 登录成功 保存 session 信息
        String token = UUIDUtil.uuid();
        setCookie(response, token, user);
        return true;
    }

    private void setCookie(HttpServletResponse response, String token, SeckillUser user) {
        redisService.set(SeckillUserKey.token, token, user);
        Cookie cookie = new Cookie(COOKIE_NAME_TOKEN, token);
        cookie.setMaxAge(SeckillUserKey.token.expireSeconds());
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    public SeckillUser getByToken(HttpServletResponse response, String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        SeckillUser user = redisService.get(SeckillUserKey.token, token, SeckillUser.class);
        if (user != null) {
            setCookie(response, token, user);
            return user;
        }
        return null;
    }
}
